#include <stdio.h>
#include <REGX52.H>
#include "I2C.h"
#include "Delay.h"
#include "Time.h"
#include "LED_8.h"

unsigned char count=1,msten=0,s=0,min=0;

void Time1() interrupt 3
{
	TL1=(65536-9174)%256;
	TH1=(65536-9174)/256;
	msten++;
	if(msten>=100)
	{
		msten%=100;
		s++;
		if(s>=60)
		{
			s=0;
			min++;
		}
	}
}

void code Show()
{
		LED_8(7,msten%10);
		Delay1ms(6);
		LED_8(6,msten/10);
		Delay1ms(2);
		P2_2=0;P2_3=1;P2_4=0;P0=0x40;
		Delay1ms(2);
		LED_8(4,s%10);
		Delay1ms(2);
		LED_8(3,s/10);
		Delay1ms(2);
		P2_2=1;P2_3=0;P2_4=1;P0=0x40;
		Delay1ms(2);
		LED_8(1,min%10);
		Delay1ms(2);
		LED_8(0,min/10);
		Delay1ms(2);	
}

int main(void)
{
	while(1)
	{	
		Show();
		if(P3_1==0)
		{
			Delay1ms(10);
			while(P3_1==0)Show();
			Delay1ms(10);
			T1_Init10ms();
			while(1)
			{
				Show();
				if(P3_0==0)
				{
					Delay1ms(10);
					while(P3_0==0)Show();
					Delay1ms(10);
					I2C_SendMessage(count++,min);
					Delay1ms(10);
					I2C_SendMessage(count++,s);
					Delay1ms(10);
					I2C_SendMessage(count++,msten);
					Delay1ms(10);
				}
				if(P3_2==0)
				{
					TMOD=0x00;
					EA=0;
					ET1=0;
					TR1=0;
					count=1;
					while(1)
					{
						if(P3_3==0)
						{
							Delay1ms(10);
							while(P3_3==0);
							Delay1ms(10);
							min=I2C_ReceiveMessage(count++);
							s=I2C_ReceiveMessage(count++);
							msten=I2C_ReceiveMessage(count++);
						}
						Show();
					}
				}
			}
		}
	}
}
